/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import doan.quanlycanbo.portlet.NoSuchsachxuatbanException;
import doan.quanlycanbo.portlet.model.impl.sachxuatbanImpl;
import doan.quanlycanbo.portlet.model.impl.sachxuatbanModelImpl;
import doan.quanlycanbo.portlet.model.sachxuatban;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the sachxuatban service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link sachxuatbanUtil} to access the sachxuatban persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Nguyen Tuan Vinh
 * @see sachxuatbanPersistence
 * @see sachxuatbanUtil
 * @generated
 */
public class sachxuatbanPersistenceImpl extends BasePersistenceImpl<sachxuatban>
	implements sachxuatbanPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = sachxuatbanImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_BY_G_L = new FinderPath(sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
			sachxuatbanModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findByG_L",
			new String[] {
				Long.class.getName(), String.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_COUNT_BY_G_L = new FinderPath(sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
			sachxuatbanModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countByG_L",
			new String[] { Long.class.getName(), String.class.getName() });
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
			sachxuatbanModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
			sachxuatbanModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countAll", new String[0]);

	/**
	 * Caches the sachxuatban in the entity cache if it is enabled.
	 *
	 * @param sachxuatban the sachxuatban to cache
	 */
	public void cacheResult(sachxuatban sachxuatban) {
		EntityCacheUtil.putResult(sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
			sachxuatbanImpl.class, sachxuatban.getPrimaryKey(), sachxuatban);
	}

	/**
	 * Caches the sachxuatbans in the entity cache if it is enabled.
	 *
	 * @param sachxuatbans the sachxuatbans to cache
	 */
	public void cacheResult(List<sachxuatban> sachxuatbans) {
		for (sachxuatban sachxuatban : sachxuatbans) {
			if (EntityCacheUtil.getResult(
						sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
						sachxuatbanImpl.class, sachxuatban.getPrimaryKey(), this) == null) {
				cacheResult(sachxuatban);
			}
		}
	}

	/**
	 * Clears the cache for all sachxuatbans.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(sachxuatbanImpl.class.getName());
		EntityCacheUtil.clearCache(sachxuatbanImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the sachxuatban.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(sachxuatban sachxuatban) {
		EntityCacheUtil.removeResult(sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
			sachxuatbanImpl.class, sachxuatban.getPrimaryKey());
	}

	/**
	 * Creates a new sachxuatban with the primary key. Does not add the sachxuatban to the database.
	 *
	 * @param masxb the primary key for the new sachxuatban
	 * @return the new sachxuatban
	 */
	public sachxuatban create(long masxb) {
		sachxuatban sachxuatban = new sachxuatbanImpl();

		sachxuatban.setNew(true);
		sachxuatban.setPrimaryKey(masxb);

		return sachxuatban;
	}

	/**
	 * Removes the sachxuatban with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the sachxuatban to remove
	 * @return the sachxuatban that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a sachxuatban with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public sachxuatban remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the sachxuatban with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param masxb the primary key of the sachxuatban to remove
	 * @return the sachxuatban that was removed
	 * @throws doan.quanlycanbo.portlet.NoSuchsachxuatbanException if a sachxuatban with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public sachxuatban remove(long masxb)
		throws NoSuchsachxuatbanException, SystemException {
		Session session = null;

		try {
			session = openSession();

			sachxuatban sachxuatban = (sachxuatban)session.get(sachxuatbanImpl.class,
					new Long(masxb));

			if (sachxuatban == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + masxb);
				}

				throw new NoSuchsachxuatbanException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					masxb);
			}

			return remove(sachxuatban);
		}
		catch (NoSuchsachxuatbanException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected sachxuatban removeImpl(sachxuatban sachxuatban)
		throws SystemException {
		sachxuatban = toUnwrappedModel(sachxuatban);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, sachxuatban);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
			sachxuatbanImpl.class, sachxuatban.getPrimaryKey());

		return sachxuatban;
	}

	public sachxuatban updateImpl(
		doan.quanlycanbo.portlet.model.sachxuatban sachxuatban, boolean merge)
		throws SystemException {
		sachxuatban = toUnwrappedModel(sachxuatban);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, sachxuatban, merge);

			sachxuatban.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
			sachxuatbanImpl.class, sachxuatban.getPrimaryKey(), sachxuatban);

		return sachxuatban;
	}

	protected sachxuatban toUnwrappedModel(sachxuatban sachxuatban) {
		if (sachxuatban instanceof sachxuatbanImpl) {
			return sachxuatban;
		}

		sachxuatbanImpl sachxuatbanImpl = new sachxuatbanImpl();

		sachxuatbanImpl.setNew(sachxuatban.isNew());
		sachxuatbanImpl.setPrimaryKey(sachxuatban.getPrimaryKey());

		sachxuatbanImpl.setMasxb(sachxuatban.getMasxb());
		sachxuatbanImpl.setGroupId(sachxuatban.getGroupId());
		sachxuatbanImpl.setCompanyId(sachxuatban.getCompanyId());
		sachxuatbanImpl.setLanguageId(sachxuatban.getLanguageId());
		sachxuatbanImpl.setMahoso(sachxuatban.getMahoso());
		sachxuatbanImpl.setTensach(sachxuatban.getTensach());
		sachxuatbanImpl.setNamxb(sachxuatban.getNamxb());

		return sachxuatbanImpl;
	}

	/**
	 * Finds the sachxuatban with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the sachxuatban to find
	 * @return the sachxuatban
	 * @throws com.liferay.portal.NoSuchModelException if a sachxuatban with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public sachxuatban findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the sachxuatban with the primary key or throws a {@link doan.quanlycanbo.portlet.NoSuchsachxuatbanException} if it could not be found.
	 *
	 * @param masxb the primary key of the sachxuatban to find
	 * @return the sachxuatban
	 * @throws doan.quanlycanbo.portlet.NoSuchsachxuatbanException if a sachxuatban with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public sachxuatban findByPrimaryKey(long masxb)
		throws NoSuchsachxuatbanException, SystemException {
		sachxuatban sachxuatban = fetchByPrimaryKey(masxb);

		if (sachxuatban == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + masxb);
			}

			throw new NoSuchsachxuatbanException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				masxb);
		}

		return sachxuatban;
	}

	/**
	 * Finds the sachxuatban with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the sachxuatban to find
	 * @return the sachxuatban, or <code>null</code> if a sachxuatban with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public sachxuatban fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the sachxuatban with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param masxb the primary key of the sachxuatban to find
	 * @return the sachxuatban, or <code>null</code> if a sachxuatban with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public sachxuatban fetchByPrimaryKey(long masxb) throws SystemException {
		sachxuatban sachxuatban = (sachxuatban)EntityCacheUtil.getResult(sachxuatbanModelImpl.ENTITY_CACHE_ENABLED,
				sachxuatbanImpl.class, masxb, this);

		if (sachxuatban == null) {
			Session session = null;

			try {
				session = openSession();

				sachxuatban = (sachxuatban)session.get(sachxuatbanImpl.class,
						new Long(masxb));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (sachxuatban != null) {
					cacheResult(sachxuatban);
				}

				closeSession(session);
			}
		}

		return sachxuatban;
	}

	/**
	 * Finds all the sachxuatbans where groupId = &#63; and languageId = &#63;.
	 *
	 * @param groupId the group id to search with
	 * @param languageId the language id to search with
	 * @return the matching sachxuatbans
	 * @throws SystemException if a system exception occurred
	 */
	public List<sachxuatban> findByG_L(long groupId, String languageId)
		throws SystemException {
		return findByG_L(groupId, languageId, QueryUtil.ALL_POS,
			QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the sachxuatbans where groupId = &#63; and languageId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param groupId the group id to search with
	 * @param languageId the language id to search with
	 * @param start the lower bound of the range of sachxuatbans to return
	 * @param end the upper bound of the range of sachxuatbans to return (not inclusive)
	 * @return the range of matching sachxuatbans
	 * @throws SystemException if a system exception occurred
	 */
	public List<sachxuatban> findByG_L(long groupId, String languageId,
		int start, int end) throws SystemException {
		return findByG_L(groupId, languageId, start, end, null);
	}

	/**
	 * Finds an ordered range of all the sachxuatbans where groupId = &#63; and languageId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param groupId the group id to search with
	 * @param languageId the language id to search with
	 * @param start the lower bound of the range of sachxuatbans to return
	 * @param end the upper bound of the range of sachxuatbans to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of matching sachxuatbans
	 * @throws SystemException if a system exception occurred
	 */
	public List<sachxuatban> findByG_L(long groupId, String languageId,
		int start, int end, OrderByComparator orderByComparator)
		throws SystemException {
		Object[] finderArgs = new Object[] {
				groupId, languageId,
				
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<sachxuatban> list = (List<sachxuatban>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_G_L,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(4 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_SACHXUATBAN_WHERE);

			query.append(_FINDER_COLUMN_G_L_GROUPID_2);

			if (languageId == null) {
				query.append(_FINDER_COLUMN_G_L_LANGUAGEID_1);
			}
			else {
				if (languageId.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_G_L_LANGUAGEID_3);
				}
				else {
					query.append(_FINDER_COLUMN_G_L_LANGUAGEID_2);
				}
			}

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(groupId);

				if (languageId != null) {
					qPos.add(languageId);
				}

				list = (List<sachxuatban>)QueryUtil.list(q, getDialect(),
						start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_G_L,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_G_L,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Finds the first sachxuatban in the ordered set where groupId = &#63; and languageId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param groupId the group id to search with
	 * @param languageId the language id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the first matching sachxuatban
	 * @throws doan.quanlycanbo.portlet.NoSuchsachxuatbanException if a matching sachxuatban could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public sachxuatban findByG_L_First(long groupId, String languageId,
		OrderByComparator orderByComparator)
		throws NoSuchsachxuatbanException, SystemException {
		List<sachxuatban> list = findByG_L(groupId, languageId, 0, 1,
				orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(6);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("groupId=");
			msg.append(groupId);

			msg.append(", languageId=");
			msg.append(languageId);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchsachxuatbanException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the last sachxuatban in the ordered set where groupId = &#63; and languageId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param groupId the group id to search with
	 * @param languageId the language id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the last matching sachxuatban
	 * @throws doan.quanlycanbo.portlet.NoSuchsachxuatbanException if a matching sachxuatban could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public sachxuatban findByG_L_Last(long groupId, String languageId,
		OrderByComparator orderByComparator)
		throws NoSuchsachxuatbanException, SystemException {
		int count = countByG_L(groupId, languageId);

		List<sachxuatban> list = findByG_L(groupId, languageId, count - 1,
				count, orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(6);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("groupId=");
			msg.append(groupId);

			msg.append(", languageId=");
			msg.append(languageId);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchsachxuatbanException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the sachxuatbans before and after the current sachxuatban in the ordered set where groupId = &#63; and languageId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param masxb the primary key of the current sachxuatban
	 * @param groupId the group id to search with
	 * @param languageId the language id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the previous, current, and next sachxuatban
	 * @throws doan.quanlycanbo.portlet.NoSuchsachxuatbanException if a sachxuatban with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public sachxuatban[] findByG_L_PrevAndNext(long masxb, long groupId,
		String languageId, OrderByComparator orderByComparator)
		throws NoSuchsachxuatbanException, SystemException {
		sachxuatban sachxuatban = findByPrimaryKey(masxb);

		Session session = null;

		try {
			session = openSession();

			sachxuatban[] array = new sachxuatbanImpl[3];

			array[0] = getByG_L_PrevAndNext(session, sachxuatban, groupId,
					languageId, orderByComparator, true);

			array[1] = sachxuatban;

			array[2] = getByG_L_PrevAndNext(session, sachxuatban, groupId,
					languageId, orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected sachxuatban getByG_L_PrevAndNext(Session session,
		sachxuatban sachxuatban, long groupId, String languageId,
		OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_SACHXUATBAN_WHERE);

		query.append(_FINDER_COLUMN_G_L_GROUPID_2);

		if (languageId == null) {
			query.append(_FINDER_COLUMN_G_L_LANGUAGEID_1);
		}
		else {
			if (languageId.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_G_L_LANGUAGEID_3);
			}
			else {
				query.append(_FINDER_COLUMN_G_L_LANGUAGEID_2);
			}
		}

		if (orderByComparator != null) {
			String[] orderByFields = orderByComparator.getOrderByFields();

			if (orderByFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		qPos.add(groupId);

		if (languageId != null) {
			qPos.add(languageId);
		}

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByValues(sachxuatban);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<sachxuatban> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Finds all the sachxuatbans.
	 *
	 * @return the sachxuatbans
	 * @throws SystemException if a system exception occurred
	 */
	public List<sachxuatban> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the sachxuatbans.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of sachxuatbans to return
	 * @param end the upper bound of the range of sachxuatbans to return (not inclusive)
	 * @return the range of sachxuatbans
	 * @throws SystemException if a system exception occurred
	 */
	public List<sachxuatban> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the sachxuatbans.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of sachxuatbans to return
	 * @param end the upper bound of the range of sachxuatbans to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of sachxuatbans
	 * @throws SystemException if a system exception occurred
	 */
	public List<sachxuatban> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<sachxuatban> list = (List<sachxuatban>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_SACHXUATBAN);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_SACHXUATBAN;
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<sachxuatban>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<sachxuatban>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
						list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the sachxuatbans where groupId = &#63; and languageId = &#63; from the database.
	 *
	 * @param groupId the group id to search with
	 * @param languageId the language id to search with
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByG_L(long groupId, String languageId)
		throws SystemException {
		for (sachxuatban sachxuatban : findByG_L(groupId, languageId)) {
			remove(sachxuatban);
		}
	}

	/**
	 * Removes all the sachxuatbans from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (sachxuatban sachxuatban : findAll()) {
			remove(sachxuatban);
		}
	}

	/**
	 * Counts all the sachxuatbans where groupId = &#63; and languageId = &#63;.
	 *
	 * @param groupId the group id to search with
	 * @param languageId the language id to search with
	 * @return the number of matching sachxuatbans
	 * @throws SystemException if a system exception occurred
	 */
	public int countByG_L(long groupId, String languageId)
		throws SystemException {
		Object[] finderArgs = new Object[] { groupId, languageId };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_L,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(3);

			query.append(_SQL_COUNT_SACHXUATBAN_WHERE);

			query.append(_FINDER_COLUMN_G_L_GROUPID_2);

			if (languageId == null) {
				query.append(_FINDER_COLUMN_G_L_LANGUAGEID_1);
			}
			else {
				if (languageId.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_G_L_LANGUAGEID_3);
				}
				else {
					query.append(_FINDER_COLUMN_G_L_LANGUAGEID_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(groupId);

				if (languageId != null) {
					qPos.add(languageId);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_L, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Counts all the sachxuatbans.
	 *
	 * @return the number of sachxuatbans
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_SACHXUATBAN);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the sachxuatban persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.doan.quanlycanbo.portlet.model.sachxuatban")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<sachxuatban>> listenersList = new ArrayList<ModelListener<sachxuatban>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<sachxuatban>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(sachxuatbanImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
	}

	@BeanReference(type = khoidonviquanlyPersistence.class)
	protected khoidonviquanlyPersistence khoidonviquanlyPersistence;
	@BeanReference(type = donviPersistence.class)
	protected donviPersistence donviPersistence;
	@BeanReference(type = chucvuPersistence.class)
	protected chucvuPersistence chucvuPersistence;
	@BeanReference(type = capuyPersistence.class)
	protected capuyPersistence capuyPersistence;
	@BeanReference(type = danhhieuPersistence.class)
	protected danhhieuPersistence danhhieuPersistence;
	@BeanReference(type = dantocPersistence.class)
	protected dantocPersistence dantocPersistence;
	@BeanReference(type = hochamPersistence.class)
	protected hochamPersistence hochamPersistence;
	@BeanReference(type = hocviPersistence.class)
	protected hocviPersistence hocviPersistence;
	@BeanReference(type = tongiaoPersistence.class)
	protected tongiaoPersistence tongiaoPersistence;
	@BeanReference(type = xuatthanPersistence.class)
	protected xuatthanPersistence xuatthanPersistence;
	@BeanReference(type = giaoducphothongPersistence.class)
	protected giaoducphothongPersistence giaoducphothongPersistence;
	@BeanReference(type = ngachluongPersistence.class)
	protected ngachluongPersistence ngachluongPersistence;
	@BeanReference(type = hesoluongPersistence.class)
	protected hesoluongPersistence hesoluongPersistence;
	@BeanReference(type = hosoPersistence.class)
	protected hosoPersistence hosoPersistence;
	@BeanReference(type = sachxuatbanPersistence.class)
	protected sachxuatbanPersistence sachxuatbanPersistence;
	@BeanReference(type = dienbienhochamPersistence.class)
	protected dienbienhochamPersistence dienbienhochamPersistence;
	@BeanReference(type = dienbienhocviPersistence.class)
	protected dienbienhocviPersistence dienbienhocviPersistence;
	@BeanReference(type = dienbienluongPersistence.class)
	protected dienbienluongPersistence dienbienluongPersistence;
	@BeanReference(type = quatrinhdinuocngoaiPersistence.class)
	protected quatrinhdinuocngoaiPersistence quatrinhdinuocngoaiPersistence;
	@BeanReference(type = quatrinhnghiencuuPersistence.class)
	protected quatrinhnghiencuuPersistence quatrinhnghiencuuPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_SACHXUATBAN = "SELECT sachxuatban FROM sachxuatban sachxuatban";
	private static final String _SQL_SELECT_SACHXUATBAN_WHERE = "SELECT sachxuatban FROM sachxuatban sachxuatban WHERE ";
	private static final String _SQL_COUNT_SACHXUATBAN = "SELECT COUNT(sachxuatban) FROM sachxuatban sachxuatban";
	private static final String _SQL_COUNT_SACHXUATBAN_WHERE = "SELECT COUNT(sachxuatban) FROM sachxuatban sachxuatban WHERE ";
	private static final String _FINDER_COLUMN_G_L_GROUPID_2 = "sachxuatban.groupId = ? AND ";
	private static final String _FINDER_COLUMN_G_L_LANGUAGEID_1 = "sachxuatban.languageId IS NULL";
	private static final String _FINDER_COLUMN_G_L_LANGUAGEID_2 = "sachxuatban.languageId = ?";
	private static final String _FINDER_COLUMN_G_L_LANGUAGEID_3 = "(sachxuatban.languageId IS NULL OR sachxuatban.languageId = ?)";
	private static final String _ORDER_BY_ENTITY_ALIAS = "sachxuatban.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No sachxuatban exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No sachxuatban exists with the key {";
	private static Log _log = LogFactoryUtil.getLog(sachxuatbanPersistenceImpl.class);
}